package com.wego.controller;

import com.wego.bean.TableBean;
import com.wego.common.bean.PageBean;
import com.wego.converter.OrderDetailsConverter;
import com.wego.entity.domain.Goods;
import com.wego.entity.domain.Order;
import com.wego.entity.query.GoodsQuery;
import com.wego.entity.query.OrderQuery;
import com.wego.entity.vo.OrderDetailsVO;
import com.wego.service.GoodsService;
import com.wego.service.OrderItemService;
import com.wego.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @className: recepationController
 * @description: 前台控制器
 * @author: Kuangkeli
 * @date: 2023/07/16 15:53
 **/
@Controller
public class RecepationController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private OrderDetailsConverter orderDetailsConverter;

    @GetMapping("/goodsDetails")
    public String goodsDetails(Long id, Model model) {
        if (id != null) {
            Goods goods = goodsService.selectByPrimaryKey(id);
            model.addAttribute("goods", goods);
        }
        return "user/goods_details";
    }

    @GetMapping("/user_orderDetails")
    public String orderDetails(OrderQuery orderQuery, Model model) {
        if (orderQuery.getUserIds() == null || orderQuery.getUserIds().size() == 0)
            return  "user/unauthorized";
        final PageBean<Order> orderPageBean = orderService.selectPage(orderQuery);
        List<Order> orderList = orderPageBean.getResult();
        Long orderCount = orderPageBean.getTotal();
        Long unpaidCount = orderService.selectPage(OrderQuery.builder().userIds(orderQuery.getUserIds()).state(1).build() ).getTotal();
        Long unshippedCount = orderService.selectPage(OrderQuery.builder().userIds(orderQuery.getUserIds()).state(2).build()).getTotal();
        Long unreceivedCount = orderService.selectPage(OrderQuery.builder().userIds(orderQuery.getUserIds()).state(3).build()).getTotal();
        Long unevaluatedCount = orderService.selectPage(OrderQuery.builder().userIds(orderQuery.getUserIds()).state(4).build()).getTotal();
        Long stagingCount = orderService.selectPage(OrderQuery.builder().userIds(orderQuery.getUserIds()).state(5).build()).getTotal();
        model.addAttribute("unpaidCount", unpaidCount);
        model.addAttribute("unshippedCount", unshippedCount);
        model.addAttribute("unreceivedCount", unreceivedCount);
        model.addAttribute("unevaluatedCount", unevaluatedCount);
        model.addAttribute("stagingCount", stagingCount);
        model.addAttribute("orderCount", orderCount);
        model.addAttribute("orderList", orderList);
        return "user/order_details";
    }


    @GetMapping("/searchGoodsListByKeywords")
    public String autoComplete(String keywords, Model model) {
        if (keywords != null) {
            List<Goods> goodsList = goodsService.selectRecommend(keywords);
            Long count = (long) goodsList.size();
            model.addAttribute("goodsList", goodsList);
            model.addAttribute("goodsCount", count);
        }
        return "user/goods_browse";

    }

}
